﻿using System;

using HsTec.Mobile.Green.BLL;
using HsTec.Mobile.Green.Model;
using HsTec.Mobile.Green.Util;
using Baimasoft.Manage.SqlServerDAL;

public partial class ReportAnalysis_AdjustProportion : System.Web.UI.Page
{
    AdjustProportionBLL bll = new AdjustProportionBLL();
    AdjustProportion model = new AdjustProportion();
    Param MyP = new Param();

    private string sEditStatus = String.Empty;
    private string sID = String.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((string)Session["Baimasoft_Pers_ID"] == null)
        {
            Response.Redirect("../Logon.aspx");
        }
        try
        {
            sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();
            if (sEditStatus == "2")
            {
                sID = this.Request.QueryString["ID"].ToString().Trim();
            }
        }
        catch
        {

        }
        if (!IsPostBack)
        {
            BindsltArea();
            txtYearsDataBind();
            try
            {
                if (sEditStatus == "2")
                {
                    model = bll.GetModel(int.Parse(sID));
                    sltArea.Value = model.Area_ID.ToString();
                    txtYears.Text = model.Years.ToString();
                    txtProportion.Text = model.Proportion.ToString();
                }
            }
            catch
            {

            }
        }
    }
    protected void SaveData_ServerClick(object sender, EventArgs e)
    {
        string sYears = txtYears.SelectedValue.Trim();
        model.Area_ID = int.Parse(sltArea.Value);
        model.Years = sYears;
        if (!ValidateHelper.IsDecimal(txtProportion.Text.Trim(), 15, 2))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('月份调整比例必须是数字型（可保留两位小数）！');</script>");
            return;
        }
        model.Proportion = Decimal.Parse(txtProportion.Text);
        model.AddDate = DateTime.Now;
        model.UpdateDate = DateTime.Now;
        model.UpdateUser = int.Parse(Session["Baimasoft_Pers_ID"].ToString());
        if (sEditStatus == "1")
        {
            if (bll.Exists(" Area_ID='" + model.Area_ID + "' and Years='" + model.Years + "'"))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('该记录已经存在，请修改区域或者年月！');</script>");
                return;
            }
            if (bll.Add(model) == 1)
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加成功！');window.location.href='AdjustProportion.aspx?EditStatus=1';</script>");
            else
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加失败！');</script>");
        }
        else if (sEditStatus == "2")
        {
            if (bll.Exists(" Area_ID='" + model.Area_ID + "' and Years='" + model.Years + "' and AP_ID!='" + sID + "'"))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('该记录已经存在，请修改区域或者年月！');</script>");
                return;
            }
            model.AP_ID = int.Parse(sID);
            if (bll.Update(model) == 1)
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功！');window.location.href='AdjustProportionManage.aspx';</script>");
            else
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败！');</script>");
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('传参错误！');</script>");
        }
    }
    private void BindsltArea()
    {
        sltArea.DataTextField = "Name";
        sltArea.DataValueField = "ID";
        sltArea.DataSource = bll.GetAreaList();
        sltArea.DataBind();
        sltArea.Items.Insert(0, "请选择县市区域...");
        sltArea.Items[0].Value = "";
    }

    private void txtYearsDataBind()
    {
        txtYears.DataTextField = "Name";
        txtYears.DataValueField = "Name";
        txtYears.DataSource = MyP.GetCodeByParentCode("1");
        txtYears.DataBind();
        txtYears.Items.Insert(0, "请选择...");
        txtYears.Items[0].Value = "";
    }
}
